<!-- 字典数据编辑弹窗 -->
<template>
  <ele-modal :form="true" :destroy-on-close="true" :width="700" v-model="visible" :title="isUpdate ? '修改' : '新建'">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px" @submit.prevent="" :inline="true">
      <el-form-item label="父类型" prop="parentType" style="width: 100%">
        <el-select v-model="form.parentType" placeholder="请选择">
          <el-option label="Zone one" value="shanghai" />
          <el-option label="Zone two" value="beijing" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类编码" prop="dictValue">
        <el-input clearable v-model="form.dictValue" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="分类名称" prop="deptName" style="width: 100%">
        <el-input clearable v-model="form.deptName" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="节点类型" prop="">
        <el-radio-group v-model="form.nodeType">
          <el-radio value="M" label="子节点" />
          <el-radio value="C" label="叶节点" />
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum" style="width: 40%">
        <el-input-number :min="0" :max="9999" v-model="form.orderNum" placeholder="请输入" controls-position="right"
          class="ele-fluid" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="form.status" />
      </el-form-item>
      <el-form-item label="备注" style="width: 100%">
        <el-input :rows="4" type="textarea" v-model="form.remark" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { EleMessage } from 'ele-admin-plus';
import { useFormData } from '@/utils/use-form-data';
import { addDictData, updateDictData } from '@/api/system/dict-data';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object,
  /** 字典类型 */
  dictType: String
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  dictCode: void 0,
  deptName:'',
  dictLabel: '',
  dictValue: '',
  cssClass: '',
  dictSort: void 0,
  parentType: '',
  status: '0',
  nodeType: 'M',
  orderNum: 0,
  remark: ''
});

/** 表单验证规则 */
const rules = reactive({
  deptName: [
    {
      required: true,
      message: '请输入',
      type: 'string',
      trigger: 'blur'
    }
  ],
  parentType: [
    {
      required: true,
      message: '请选择',
      trigger: 'change'
    }
  ],
  dictValue: [
    {
      required: true,
      message: '请输入',
      type: 'string',
      trigger: 'blur'
    }
  ],
  dictSort: [
    {
      required: true,
      message: '请输入',
      type: 'number',
      trigger: 'blur'
    }
  ],
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateDictData : addDictData;
    saveOrUpdate({
      ...form,
      dictType: props.dictType
    })
      .then((msg) => {
        loading.value = false;
        EleMessage.success({ message: msg, plain: true });
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({ message: e.message, plain: true });
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      console.log(props.data)
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
